<?php

namespace app\system\controller;

use app\common\Uploader;
use app\model\SystemUploadfile;
use app\system\BaseController;
use app\service\SystemUploadService;
use think\File;
use think\helper\Arr;

class Upload extends BaseController
{
    public function uploadImage()
    {
        $files = request()->file();
        if (!$targetFile = Arr::get($files, 'image')) {
            return $this->error('image不能为空');
        }
        validate(['image' => 'fileSize:1024000|fileExt:jpg,png,jpeg'])->check($files);
        return $this->success((new SystemUploadService)->create($targetFile, $this->request->user->uid));
    }

    public function uploadFile()
    {
        $files = request()->file();
        /** @var File $targetFile */
        if (!$targetFile = Arr::get($files, 'file')) {
            return $this->error('file不能为空');
        }
        validate(['file' => 'fileSize:1024000|fileExt:mp3'])->check($files);
        return $this->success((new SystemUploadService)->create($targetFile, $this->request->user->uid, Uploader::FILE_PATH));
    }

    public function info()
    {
        if (!$id = $this->request->get('id')) {
            return $this->error('缺少必要参数id');
        }
        $model = SystemUploadfile::find($id);
        return $this->success($model);
    }
}